<template>
  <a-modal title-align="start" title="批量接/拒单" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="400">
    <a-radio-group v-model="accept">
      <a-radio :value="true">确认接单</a-radio>
      <a-radio :value="false">拒绝接单</a-radio>
    </a-radio-group>

    <div v-if="accept">
      <a-select style="width: 260px" allow-search @change="changePurchaser" v-model="purchaserId" placeholder="请选择采购员">
        <a-option v-for="(item, index) of userList" :key="index" :label="item.userName" :value="item.id"></a-option>
      </a-select>
    </div>

    <div class="mt10" v-if="!accept">
      <a-textarea v-model="remark" placeholder="请输入原因" />
    </div>

    <template #footer class="modal-footer">
      <a-button @click="showvisible = false">取消</a-button>
      <a-button type="primary" :loading="loading" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { onMounted, ref, computed } from 'vue'
  import { Message } from '@arco-design/web-vue'
  import { batchPurchaseTake } from '@/api/purchase'
  import { getOrganizesUser } from '@/api/system'
  let props = defineProps(['modelValue', 'ids'])
  let emits = defineEmits(['save', 'update:modelValue'])
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })

  let remark = ref('')
  let purchaserId = ref('')
  let loading = ref(false)
  let accept = ref(true)
  let purchaser = ref({})
  let userList = ref<Array<any>>([])
  const getUSerList = async () => {
    let params = {
      depart: false,
      pageNum: 1,
      pageSize: 100000,
    }
    const res: any = await getOrganizesUser(params)
    userList.value = res?.data?.records || []
  }

  const changePurchaser = (val: string) => {
    let user = userList.value.find((e) => e.id == val)
    if (user) {
      let obj = {
        deptId: user?.deptId || '',
        deptName: user?.deptName || '',
        id: user?.id || '',
        name: user?.userName || '',
      }

      purchaser.value = obj
    }
  }

  const submit = async () => {
    if (!accept.value && !remark.value) return Message.error('请填写原因')
    let params = {
      accept: accept.value,
      idList: props.ids,
      rejectReason: remark.value,
      purchaser: purchaser.value || {},
    }

    try {
      const res: any = await batchPurchaseTake(params)
      if (res) {
        Message.success(`${accept.value ? '接单' : '拒绝接单'}成功`)
        emits('save')
        showvisible.value = false
      }
    } catch (error) {}
    loading.value = false
  }

  const init = () => {
    getUSerList()
  }
  onMounted(init)
</script>

<style lang="less" scoped></style>
